package com.zx.mes.disruptor.hyl.quickstart;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

/**
 * disruptor producer
 * @author huayunlong
 * @date 2018-9-5
 */
public class OrderEventProducer {

    private RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {

        this.ringBuffer = ringBuffer;
    }

    /**
     * 官网推荐(try{}finally{})写法
     * @param buffer ByteBuffer
     */
    public void send(ByteBuffer buffer) {
        long sequence = ringBuffer.next();
        try {
            OrderEvent orderEvent = ringBuffer.get(sequence);
            orderEvent.setValue(buffer.getLong(0));
        } finally {
            ringBuffer.publish(sequence);
        }

    }
}
